package com.shs.riggy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shs.riggy.common.MyCustomException;
import com.shs.riggy.dto.DishDto;
import com.shs.riggy.entity.Dish;
import com.shs.riggy.entity.DishFlavor;
import com.shs.riggy.mapper.DishMapper;
import com.shs.riggy.service.DishFlavorService;
import com.shs.riggy.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 晚点再动心
 * @Date 2022/9/13
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * 保存添加菜品时候的口味信息
     *
     * @param dishDto 菜dto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存基本的菜品信息到dish表
        this.save(dishDto);
        Long dishId = dishDto.getId();
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item) ->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByDishIdWithFlavor(Long dishId) {
        //查询菜品基本信息，从dish表
        Dish dish = this.getById(dishId);
        DishDto dishDto = new DishDto();
        //对象拷贝。最终返回的是需要一个dishDto
        BeanUtils.copyProperties(dish,dishDto);
        //  从dish_flavor表查询 当前菜品对应的口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishId);

        //查询出的口味集合
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);

        dishDto.setFlavors(flavors);
        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //跟新dish表的基本信息
        this.updateById(dishDto);
        //清理当前菜品对应口味数据 即为从dish-flavors表中删除原有信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //Long dishId = dishDto.getId();
        //添加当前提交过来的口味数据到dish-flavors表
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item) ->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);

    }

    @Override
    public void batchDeleteByIds(List<Long> ids) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ids != null,Dish::getId,ids);

        //  mybatisplus提供了 list方法，故 this.list(queryWrapper); -->dishService.list(queryWrapper);
        List<Dish> list = this.list(queryWrapper);

        if (list != null){
            for (Dish dish : list) {
                if (dish.getStatus() == 0){
                    this.removeByIds(ids);
                }else {
                    throw new MyCustomException("有菜品正在售卖，无法全部删除！");
                }
            }
        }


    }
}
